<?php
print '<h1>' . __('Wanted to know who are the stars?', true) . '</h1>';
print '<p>' . __('Just give us your favourite twitterers and we\'ll tell you who everyone is following.', true) . '</p>';

print $form->create('Star', array('action' => 'index', 'type' => 'get'));
print $form->input('theNames', array(
											'label' => false,
											'value' => isset($userList) ? $userList : null,
											'title' => __('Comma or space separated list of twitter user names', true),
											'div' => null
										)
								);
print $form->submit(__('Send', true),
										array(
											'div' => false,
											'id' => 'StarSearch'
										)
									);
print $html->image('ajax-loader.gif', array('id'=>'loader', 'style'=>'display:none;'));
print $form->end();

print '<div id="ads">';
if(Configure::read('Config.language') == 'hu'){
	print $html->link($html->image('webmania_logo.png', array('alt' => 'WebMania.cc')), 'http://webmania.cc', null, null, false);
	print $html->link($html->image('twittertop_logo.png', array('alt' => 'TwitterTop.hu')), 'http://twittertop.hu', null, null, false);
}
print '</div>';

$twitteverest = $session->read('twitteverest');
if(isset($twitteverest['stars']['error'])){
	print __('Some error has occured. Please try again later. You could also send us some', true) . ' ' . $html->link(__('Feedback', true), array('controller' => 'stars', 'action' => 'feedback'), null, null, true);
}
elseif($twitteverest){	
	if(isset($twitteverest['stars']['warning'])){
		if($twitteverest['stars']['warning']['noJointStars']){
			print '<p class="warning">' . __('No one is followed by everybody. Remove one or more users from the search list.', true) . '</p>';
		}
		else{
			print '<h3>' . sprintf(__('%d random stars', true), ($twitteverest['starCount'])) . '</h3>';
			if($twitteverest['stars']['warning']['tooMuchStars']){
				print '<p class="warning">' . sprintf(__('Too much stars! (%d) Please add some more to the search box.', true), $twitteverest['starCountAll']) . '</p>';
			}
			if(isset($twitteverest['stars']['warning']['userDoesNotExists'])){
				$invalidUsers = implode(', ', $twitteverest['stars']['warning']['userDoesNotExists']);
				print '<p class="warning">' . sprintf(__('%s seems to not to be valid twitter username(s). We have skipped it/them from the search.', true), $invalidUsers) . '</p>';
			}
		}
	}
	else
		print '<h3>' . sprintf(__('%d stars followed by everyone', true) , ($twitteverest['starCount'])) . '</h3>';

	print '<ul>';
	
	$userLink = ($session->read('Config.language') == 'hu') ? 'http://turulcsirip.hu/user/' : 'http://twitter.com/';
	
	$m = ($twitteverest['starPerPage']*$page);
	for($i = ($page-1)*$twitteverest['starPerPage']; $i < $m; $i++){
		if(isset($twitteverest['stars'][$i])){
			$t = $twitteverest['stars'][$i];
			if($t->screen_name){
				print '<li class=user>';
					print '<span class="action">';
					print '<a href="'.$userLink.$t->screen_name.'" title="' . sprintf(__('See %s', true), $t->screen_name) . '"><img src="'.$t->profile_image_url.'" width="50" height="50" /></a>';
					print '<button value="' . $t->screen_name . '" class="addbut" title="' . __('Add the user list above', true) . '">' . __('Add', true) . '</button>';
					print '</span>';
						/*
						 http://twitter.com/friendships/create/1431771
						 authenticity_token	: 27846f6080964888af3be704d5466b64ab1f9d5c
						 twttr	: true
						 _('Follow')
						*/
					$statusesCount = isset($t->statuses_count) ? $t->statuses_count : 0;
					print '<ul class="info">';
						print '<li><span class="data">' . __('Name: ', true) . '</span> <a href="'.$userLink.$t->screen_name.'" title="' . sprintf(__('See %s', true), $t->screen_name) . '">' . $t->name . '</a></li>';
						print '<li><span class="data">' . __('Number of followers: ', true) . '</span> ' . number_format($t->followers_count, 0, '.', ',') . '</li>';
						print '<li><span class="data">' . __('Updates: ', true) . '</span> ' . number_format($statusesCount, 0, '.', ',') . '</li>';
						if(isset($t->created_at))
							print '<li><span class="data">' . __('Twitt/day: ', true) . '</span> ' . number_format(($statusesCount/((time()-strtotime($t->created_at))/(60*60*24))), 0, '.', ',') . '</li>';
						if(isset($t->friends_count))
							print '<li><span class="data">' . __('Number of friends: ', true) . '</span> ' . number_format($t->friends_count, 0, '.', ',') . '</li>';
						if($t->url){
							$url = str_replace('http://www.', '', $t->url);
							$url = str_replace('http://', '', $url);
							print '<li><span class="data">' . __('Url: ', true) . '</span> <a href="' . $t->url . '">' . substr($url, 0, 20) . '</a></li>';
						}
						print '<li><span class="data">' . __('Twitter name: ', true) . '</span> ' . $t->screen_name . '</li>';
					print '</ul>';
				print '</li>';
			}
		}
	}
	print '</ul>';
		
	if($twitteverest['starCount'] > $twitteverest['starPerPage']){
		print '<ul id="pager">';
			$pages = $twitteverest['starCount']/$twitteverest['starPerPage'];
			for($i = 0; $i < $pages; $i++){
				$akt = (($i+1) == $page) ? true : false;
				print '<li>';
					if($akt)
						print ($i + 1);
					else
						print $html->link(($i + 1), array('action'=> '?page=' . ($i + 1)));
				print '</li>';
			}
		print '</ul>';
	}
}

?>